package com.wuzhishan.news.utils;

import com.wuzhishan.news.mapper.commentMapper;
import com.wuzhishan.news.mapper.favorMapper;
import com.wuzhishan.news.pojo.ArticleVo;
import com.wuzhishan.news.pojo.BlogArticle;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

public class strengthArticle {
    @Autowired
    private static commentMapper commentMapper;
    @Autowired
    private static favorMapper favorMapper;

    public static List articleVo(List list,List<BlogArticle> articles){
        for (BlogArticle article : articles) {
            ArticleVo vo = new ArticleVo();
            /**
             *将当前文章评论数缓存，防止高并发问题
             * 使用Ehcache做缓存
             */
            Integer counts = EhcacheUtil.get("Cache1","articleComments");
            if (counts == null){
                counts = commentMapper.countsOfArticle(article.getId());
                vo.setComments(counts);
                EhcacheUtil.put("Cache1","articleComments",counts,5,5);
            }
            /**
             *当前文章喜欢数缓存，防止高并发
             * 使用Ehcache做缓存
             */
            Integer likes = EhcacheUtil.get("Cache1","likes");
            if (likes == null){
                likes = favorMapper.articleFavors(article.getId());
                vo.setLikes(likes);
                EhcacheUtil.put("Cache1","likes",likes,5,5);
            }
            BeanUtils.copyProperties(article,vo);
            list.add(vo);
        }
        return list;
    }
}
